package com.fany.utils;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Locale;

public class HttpHelper
{

	private static HttpURLConnection createHttpURLConnection(String url, String method, String contentType,
                                                             String encode)
	{
		HttpURLConnection connection = null;
		try
		{
			URL urlObject;
			urlObject = new URL(url);
			connection = (HttpURLConnection) urlObject.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			if (method.toUpperCase(Locale.CHINA).equals("post"))
			{
				connection.setRequestMethod("post");
				connection.setRequestProperty("Content-type", contentType);
				connection.setRequestProperty("Accept-Charset", encode);
				connection.setRequestProperty("contentType", encode);
				connection.setInstanceFollowRedirects(true);
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return connection;
	}

	public static String get(String url, String contentType, String encode)
	{
		String resultData = "";
		try
		{
			HttpGet getMethod = new HttpGet(url);
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(getMethod);
			resultData = EntityUtils.toString(response.getEntity(), encode);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return resultData;
	}

	public static String getLocation(String reqUrl)
	{
		String location = null;
		try
		{
			DefaultHttpClient httpclient = new DefaultHttpClient();
			final HttpGet request = new HttpGet(reqUrl);
			HttpParams params = new BasicHttpParams();
			params.setParameter("http.protocol.handle-redirects", false);
			request.setParams(params);
			HttpResponse response = httpclient.execute(request);
			int responseCode = response.getStatusLine().getStatusCode();
			if (responseCode == 302)
			{
				Header locationHeader = response.getFirstHeader("Location");
				if (locationHeader != null)
				{
					location = locationHeader.getValue();
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return location;
	}

	public static String post(String url, String contentType, String content, String encode)
	{
		String resultData = "";
		try
		{
			HttpURLConnection connection = createHttpURLConnection(url, "post", contentType, encode);
			connection.setRequestProperty("Content-Type", contentType);
			connection.connect();
			DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
			outputStream.writeBytes(content);
			outputStream.flush();
			outputStream.close();
			connection.disconnect();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return resultData;
	}

	// 转换为%E4%BD%A0形式
	public static String textToURL(String s, String encoding)
	{
		String result = "";
		try
		{
			result = URLEncoder.encode(s, encoding);
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return result;
	}

	// 将%E4%BD%A0转换为汉字
	public static String urlToText(String s, String encoding)
	{
		String result = "";
		try
		{
			result = URLDecoder.decode(s, encoding);
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return result;
	}
}
